package com.tzy.user.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tzy.common.constant.CacheConstant;
import com.tzy.common.constant.WebResult;
import com.tzy.common.constant.WebResultMsgCode;
import com.tzy.common.entity.User;
import com.tzy.common.utils.RandomUtil;
import com.tzy.user.service.IUserService;
import com.tzy.user.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
 * 发送邮件控制器
 */
@Slf4j
@RestController
@RequestMapping("/user/mail")
public class MailController {
    @Resource
    private JavaMailSender mailSender;
    @Resource
    private RedisService redisService;
    @Resource
    private IUserService iUserService;
    @Value("${spring.mail.sender}")
    private String emailSender;//邮件发送方
    /**
     * 发送邮件
     * @param email
     * @return
     * @throws MessagingException
     */
    @GetMapping("/send")
    public WebResult sendMail(@RequestParam(value = "email") String email)
            throws MessagingException {
        log.info("email="+ email);
        String checkCode = RandomUtil.getCheckCode();
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mmh = new MimeMessageHelper(mimeMessage,true);
        //发送方的邮箱
        mmh.setFrom( emailSender );
        //接收方的邮箱
        mmh.setTo(email.trim());//去除两端空格
        mmh.setSubject("欢迎注册饱了么app");
        mmh.setText("<!DOCTYPE html>\n" +
                "<html>\n" +
                "\t<head>\n" +
                "\t\t<meta charset=\"utf-8\">\n" +
                "\t\t<title></title>\n" +
                "\t</head>\n" +
                "\t<body>\n" +
                "\t\t<p>您的验证码是:</p>\n" +
                "\t\t<p>"+checkCode+"</p>\n" +
                "\t\t<p>验证码的有效期为15分钟</p>\n" +
                "\t\t<p>如非本人操作, 请忽略</p>\n" +
                "\t</body>\n" +
                "</html>",true);
        try {
            mailSender.send(mimeMessage);
            //存到redis里面, 并设置过期时间为 15分钟
            redisService.setString(CacheConstant.CACHE_CHECK_CODE+email,checkCode,60*15);
            log.info("redis checkCode="+redisService.getString(CacheConstant.CACHE_CHECK_CODE+email));
            return new WebResult(WebResultMsgCode.SUCCESS,"success");
        } catch (Exception e) {
            log.error("发送邮件时发生异常了！", e);
            return new WebResult(WebResultMsgCode.FAIL);
        }finally{
            log.info("邮件已发送。");
        }
    }

    /**
     * 发送审核拒绝信息
     * @param denyInfo 拒绝信息
     * @return
     */
    @GetMapping("sendAuditInfo")
    public WebResult sendAuditInfo(String denyInfo, int userId, String shopName)
            throws MessagingException {
        log.info("denyInfo="+ denyInfo);
        //根据Id获取用户邮箱
        User user = iUserService.getOne(new QueryWrapper<User>().eq("id", userId));
        String email = user.getEmail();

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mmh = new MimeMessageHelper(mimeMessage,true);
        //发送方的邮箱
        mmh.setFrom( emailSender );
        //接收方的邮箱
        mmh.setTo(email.trim());//去除两端空格
        mmh.setSubject("欢迎申请饱了么商家");
        mmh.setText("<!DOCTYPE html>\n" +
                "<html>\n" +
                "\t<head>\n" +
                "\t\t<meta charset=\"utf-8\">\n" +
                "\t\t<title></title>\n" +
                "\t</head>\n" +
                "\t<body>\n" +
                "\t<h2>尊敬的用户："+email+"</h2>\n" +
                "\t\t<p>您好</p>\n" +
                "\t<p>您的店铺："+shopName+"申请被拒绝:</p>\n" +
                "\t<p>原因："+denyInfo+"</p>\n" +
                "\t</body>\n" +
                "</html>",true);
        try {
            mailSender.send(mimeMessage);
            return new WebResult(WebResultMsgCode.SUCCESS,"success");
        } catch (Exception e) {
            log.error("发送邮件时发生异常了！", e);
            return new WebResult(WebResultMsgCode.FAIL);
        }finally{
            log.info("邮件已发送。");
        }
    }


    public static void main(String[] args) {
//        System.out.println("验证码:"+RandomUtil.getCheckCode());;
        System.out.println("邀请码:"+RandomUtil.getInvitationCode());;
    }

}
